package com.example.appointment.common.http;

import android.content.Context;

import com.example.appointment.BuildConfig;
import com.squareup.okhttp.OkHttpClient;

import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.concurrent.TimeUnit;

/**
 * Created by LGM on 2015/12/2.
 */
public class HttpClientManager {


    public static String userAgent = "cheyoujibussiness/" + BuildConfig.VERSION_NAME + "/android/";

    private static HttpClientManager httpClientManager;


    private HttpClientManager() {
    }

    private static OkHttpClient okHttpClient;


    public static HttpClientManager getInstance() {
        if (httpClientManager == null) {


            httpClientManager = new HttpClientManager();
        }
        return httpClientManager;
    }


    public void initOkHttpClient(Context context) {
        okHttpClient = getOkHttpClient();
        okHttpClient.setCookieHandler(new CookieManager(
                new PersistentCookieStore(context),
                CookiePolicy.ACCEPT_ALL));
    }

    public OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
        }
        okHttpClient.setConnectTimeout(20, TimeUnit.SECONDS);
        okHttpClient.setReadTimeout(20, TimeUnit.SECONDS);
        return okHttpClient;
    }

    public void initCleanClient() {
        okHttpClient = new OkHttpClient();
        okHttpClient.setConnectTimeout(20, TimeUnit.SECONDS);
        okHttpClient.setReadTimeout(20, TimeUnit.SECONDS);
    }


}
